﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Lswweb.Domain;

namespace Lswweb.Controls
{
    /// <summary>
    /// DropDownList
    /// </summary>
    /// <typeparam name="T">Enum类型</typeparam>
    public abstract class EnumDropDownList<T> : DropDownList
    {
        protected override void OnInit(EventArgs e)
        {
            Enum<T>.BinderToListControl(this);

            if (!string.IsNullOrEmpty(DefaultItemText))
                Items.Insert(0, new ListItem(DefaultItemText, DefaultItemValue));
        }

        public new T SelectedValue
        {
            get
            {
                EnsureChildControls();
                return (T)Enum.Parse(typeof(T), base.SelectedValue);
            }
            set
            {
                EnsureChildControls();
                ListItem item = Items.FindByValue(Convert.ToInt32(value).ToString());
                if (item != null)
                    this.SelectedIndex = Items.IndexOf(item);
            }
        }

        public string DefaultItemText { get; set; }

        private string _defaultItemValue = "-1";
        public string DefaultItemValue 
        {
            get { return _defaultItemValue; }
            set { value = _defaultItemValue; }
        }
    }

    /// <summary>
    /// CheckBoxList
    /// </summary>
    /// <typeparam name="T">Enum类型</typeparam>
    public abstract class EnumCheckBoxList<T> : CheckBoxList
    {
        public EnumCheckBoxList()
        {
            Enum<T>.BinderToListControl(this);
        }

        public new IList<T> SelectedValue
        {
            get
            {
                EnsureChildControls();

                IList<T> list = new List<T>();
                foreach (ListItem item in Items)
                {
                    if (item.Selected)
                        list.Add((T)Enum.Parse(typeof(T), item.Value));
                }
                return list;
            }
            set
            {
                EnsureChildControls();
                foreach (ListItem item in Items)
                {
                    if (value.Contains((T)Enum.Parse(typeof(T), item.Value)))
                        item.Selected = true;
                }
            }
        }
    }

    /// <summary>
    /// RadioButtonList
    /// </summary>
    /// <typeparam name="T">Enum类型</typeparam>
    public abstract class EnumRadioButttonList<T> : RadioButtonList
    {
        public EnumRadioButttonList()
        {
            Enum<T>.BinderToListControl(this);    
        }

        protected override void OnLoad(EventArgs e)
        {
            if(!Page.IsPostBack && this.Items.Count>0)
                this.Items[0].Selected = true;
        }

        public new T SelectedValue
        {
            get
            {
                EnsureChildControls();
                return (T)Enum.Parse(typeof(T), base.SelectedValue);
            }
            set
            {
                EnsureChildControls();
                ListItem item = Items.FindByValue(Convert.ToInt32(value).ToString());
                if (item != null)
                    this.SelectedIndex = Items.IndexOf(item);
            }
        }
    }
}
